HAL CORTEX initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 CORTEX configuration panel.

  • Configure the CORTEX MPU.

  • Configure the CORTEX NVIC.

  • Configure the CORTEX Systick.

  • Generate the code.

HAL CORTEX MPU configuration

HAL CORTEX MPU main configuration

Activate the MPU then configure the CORTEX MPU main features.

CORTEX MPU main configuration

HAL CORTEX MPU additional features

Configure the CORTEX MPU additional features.

CORTEX MPU additional config

HAL CORTEX NVIC configuration

HAL GPIO EXTI configuration

Configure the CORTEX NVIC for GPIO EXTI(or any desired peripheral).

GPIO NVIC configuration

HAL NVIC configuration

Configure the NVIC features.

NVIC list

HAL CORTEX Systick configuration

Configure the CORTEX Systick.

CORTEX Systick configuration

HAL CORTEX SYSTICK main configuration

Configure the CORTEX SYSTICK main features.

SYSTICK config

HAL CORTEX SYSTICK system configuration

Configure the CORTEX SYSTICK system configuration.

SYSTICK config

Generated CORTEX initialization and de-initialization sequences

The CORTEX initialization sequence is generated in the mx_CORTEX_hal_init function in mx_CORTEX.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concept:

STM32CubeMX2 Generated : CORTEX initialization and de-initialization sequences

Topic

HAL1 code snippet

HAL2 code snippet

MPU

void MPU_Config(void)
{
  MPU_Region_InitTypeDef MPU_InitStruct = {0};
  MPU_Attributes_InitTypeDef MPU_AttributesInit = {0};

  /* Disables the MPU */
  HAL_MPU_Disable();

  /** Initializes and configures the Region 0 and the memory to be protected
  */
  MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  MPU_InitStruct.BaseAddress = 0x20000000;
  MPU_InitStruct.LimitAddress = 0x20001FFF;
  MPU_InitStruct.AttributesIndex = MPU_ATTRIBUTES_NUMBER0;
  MPU_InitStruct.AccessPermission = MPU_REGION_ALL_RW;
  MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
  MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;

  HAL_MPU_ConfigRegion(&MPU_InitStruct);

  /** Initializes and configures the Attribute 0 and the memory to be protected
  */
  MPU_AttributesInit.Number = MPU_ATTRIBUTES_NUMBER0;
  MPU_AttributesInit.Attributes = INNER_OUTER(MPU_WRITE_THROUGH|MPU_TRANSIENT
                              |MPU_RW_ALLOCATE);

  HAL_MPU_ConfigMemoryAttributes(&MPU_AttributesInit);
  /* Enables the MPU */
  HAL_MPU_Enable(MPU_HARDFAULT_NMI);

}
system_status_t mx_cortex_mpu_init(void)
{
  /* Disables the MPU */
  HAL_CORTEX_MPU_Disable();

  /*
     Initializes and configures the MPU attributes
  */
  HAL_CORTEX_MPU_SetDeviceMemAttr(HAL_CORTEX_MPU_MEM_ATTR_0, HAL_CORTEX_MPU_DEVICE_MEM_NGNRNE);

  /*
     Initializes and configures the MPU Region
  */
  hal_cortex_mpu_region_config_t p_region_config = {0};

  p_region_config.base_addr = 0x20000000;
  p_region_config.limit_addr = 0x20001FFF;
  p_region_config.access_attr = HAL_CORTEX_MPU_REGION_ONLY_PRIV_RW;
  p_region_config.exec_attr = HAL_CORTEX_MPU_EXECUTION_ATTR_ENABLE;
  p_region_config.attr_idx = HAL_CORTEX_MPU_MEM_ATTR_0;
  HAL_CORTEX_MPU_SetConfigRegion(HAL_CORTEX_MPU_REGION_0, &p_region_config);

  HAL_CORTEX_MPU_EnableRegion(HAL_CORTEX_MPU_REGION_0);

  /* Enables the MPU */
  HAL_CORTEX_MPU_Enable(HAL_CORTEX_MPU_HARDFAULT_NMI_DISABLE, HAL_CORTEX_MPU_ACCESS_FAULT_ONLY_PRIV);

  return SYSTEM_OK;
}

NVIC

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* USER CODE BEGIN MX_GPIO_Init_1 */

  /* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();

  /*Configure GPIO pin : TEST_PIN_Pin */
  GPIO_InitStruct.Pin = TEST_PIN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(TEST_PIN_GPIO_Port, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);

  /* USER CODE BEGIN MX_GPIO_Init_2 */

  /* USER CODE END MX_GPIO_Init_2 */
}
system_status_t mx_gpio_default_init(void)
{
  hal_gpio_config_t  gpio_config;

  HAL_RCC_GPIOE_EnableClock();

  /*
    GPIO pin labels :
    PE2   ---------> PE2, TEST_PIN
    */
  /* Configure PE2 GPIO pin in input mode */
  gpio_config.mode            = HAL_GPIO_MODE_INPUT;
  gpio_config.pull            = HAL_GPIO_PULL_NO;
  if (HAL_GPIO_Init(PE2_PORT, PE2_PIN, &gpio_config) != HAL_OK)
  {
    return SYSTEM_PERIPHERAL_ERROR;
  }

  hal_exti_config_t exti_config;

  /* Initialize the EXTI for line 2 */
  HAL_EXTI_Init(&hEXTI2, HAL_EXTI_LINE_2);

  /* Set the trigger as RISING for the GPIOE */
  exti_config.trigger   = HAL_EXTI_TRIGGER_RISING;
  exti_config.gpio_port = HAL_EXTI_GPIOE;
  HAL_EXTI_SetConfig(&hEXTI2, &exti_config);

  /* Set line 2 Interrupt priority */
  HAL_CORTEX_NVIC_SetPriority(EXTI2_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(EXTI2_IRQn);

              return SYSTEM_OK;
}

SYSTICK

Managed internally in HAL1.
system_status_t mx_cortex_systick_init(void)
{
  HAL_CORTEX_SYSTICK_Suspend();

  /* System tick timer */
  HAL_CORTEX_NVIC_SetPriority(SysTick_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);

  HAL_CORTEX_SYSTICK_SetClkSource(HAL_CORTEX_SYSTICK_CLKSOURCE_INTERNAL);

  HAL_CORTEX_SYSTICK_SetFreq(HAL_TICK_FREQ_1KHZ);

  return SYSTEM_OK;
}